/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GameHallClient;

import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;
import seGobangClient.onlineHall.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;
import seGobangClient.onlineHall.*;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
/**
 *
 * @author Peach
 */
public class GameHallClient {
    public GameHallClient(){
        try {
            url = new URL("http://59.78.37.65:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = "gameHallService";
	methodQuit = "quitGame";
	methodEnter = "takeGame";
        methodGetFreeList="getFreeDeskList";
        methodExchange="exchangeTurn";
        methodGetReady="getReady";
        methodCancelReady="cancelReady";        
    }
    
    public GameHallClient(String URI,String quit,String enter,
            String getList,String exchange,String getReady,String cancelReady){
        try {
            url = new URL("http://59.78.37.65:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = URI;
	methodQuit = quit;
	methodEnter = enter;
        methodGetFreeList=getList;
        methodExchange=exchange;
        methodGetReady=getReady;
        methodCancelReady=cancelReady;
    }
    
    public PlayerInfo GetRemotePlayerInfo(int room,int desk,int id){
        
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName("checkDesk");
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("id", int.class, id, null));
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                ArrayList<Integer> deskinf=(ArrayList<Integer>)result.getValue();
                
                if(deskinf.get(0)==2){
                    
                    PlayerInfo player=null;      
                    
                    if(id!=deskinf.get(1)){
                        
                        player=GetPlayerInfo(deskinf.get(1));
                        player.ChessColor=-1;//黑子
                        
                    }else{
                        player=GetPlayerInfo(deskinf.get(2));
                        player.ChessColor=1;//white子
                        
                    }
                    
                    return player;
                    
                    
                }else{
                
                    return null;
                }
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
        
    return null;
        
    }
    
    public PlayerInfo GetPlayerInfo(int id){
    
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName("getOpponent");
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("id", int.class, id, null));        
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                ArrayList<String> playerinf=(ArrayList<String>)result.getValue();
                
                PlayerInfo player=new PlayerInfo();
                
                player.UID=id;
                player.Name=playerinf.get(0);
                player.Wins=Integer.parseInt(playerinf.get(1));
                player.Losts=Integer.parseInt(playerinf.get(2));
                return player;
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
        return null;
        
    }
    
    
    
    public void quitGame(int id, int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodQuit);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("id", int.class, id, null));
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
    }
    
    public void takeGame(int id, int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodEnter);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("id", int.class, id, null));
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void getReady(int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodGetReady);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void cancelReady(int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodCancelReady);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public ArrayList<Integer> getFreeDeskList(int room){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodGetFreeList);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("room", int.class, room, null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                System.out.println(result.getValue());
                return (ArrayList<Integer>)result.getValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	return null;
    }
    
    public void exchangeTurn(int room, int desk){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodExchange);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
        params.addElement(new Parameter("room",int.class,room,null));
        params.addElement(new Parameter("desk",int.class,desk,null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
    public static void main(String[] args){
        GameHallClient test =new GameHallClient();
        test.cancelReady(1, 3);
    }
    
    
    private URL url;
    private String uri;
    private String methodQuit;
    private String methodEnter;
    private String methodGetFreeList;
    private String methodExchange;
    private String methodGetReady;
    private String methodCancelReady;
}
